package com.stylefeng.guns.rest.common.utils;

import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesUtil {
    private static final Logger logger = Logger.getLogger(PropertiesUtil.class);
    private static Properties props = new Properties();
    private static Properties propsLoginPro = new Properties();

    static {
        InputStream input = null;
        InputStream loginInput = null;
        try {
            input = PropertiesUtil.class.getClassLoader().getResourceAsStream("pro.properties");
            props.load(input);
            /*loginInput = PropertiesUtil.class.getClassLoader().getResourceAsStream("login.properties");
            propsLoginPro.load(loginInput);*/
        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
        } finally {
            if (input != null) {
                try {
                    input.close();

                } catch (IOException e) {
                    logger.warn(e.getMessage(), e);
                }
            }
            if (loginInput != null) {
                try {
                    loginInput.close();

                } catch (IOException e) {
                    logger.warn(e.getMessage(), e);
                }
            }
        }
    }

    public static String getFromLoginPro(String key) {
        String result = propsLoginPro.getProperty(key);

        if (result != null) {
            result = result.trim();
        }

        return result;
    }

    public static String get(String key) {
        String result = props.getProperty(key);

        if (result != null) {
            result = result.trim();
        }

        return result;
    }

	/*
    public Properties readProperties(String filePath){
		TemplateSMS loadProp = new TemplateSMS();
		InputStream input = loadProp.getClass().getResourceAsStream(filePath);//获取存储信息的配置文件
		Properties info = new Properties();
		try{
			info.load(input);//读取配置文件信息
			return info;
		}catch(Exception e){
			logger.warn(e.getMessage(), e);//错误信息
			return null;
		}finally{
			if(input!=null){
				try{
					input.close();
				}catch(IOException e){
					logger.warn(e.getMessage(), e);
				}
			}
		}
	}
	
	public void updateProperties(String fileName,Map<String,String> keyValueMap){//修改配置文件的方法
		String filePath = PropertiesUtil.class.getClassLoader().getResource(fileName).getFile();
		Properties props = null;
		BufferedWriter bw = null;
		try{
			filePath = URLDecoder.decode(filePath,"utf-8");//文件路径
			logger.debug("updateProperties propertiesPath:" + filePath);
			props = PropertiesLoaderUtils.loadProperties(new ClassPathResource(fileName));
			logger.debug("updateProperties old:" + props);
			//写入属性文件
			bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath)));
			props.clear();
			for(String key :keyValueMap.keySet())
				props.setProperty(key, keyValueMap.get(key));
			logger.debug("updateProperties new:" + props);
			props.store(bw, "");
		}catch(IOException e){
			logger.error(e.getMessage(), e);
		}finally{
			if (bw != null) {
				try{
					bw.close();
				}catch(IOException e){
					logger.error(e.getMessage(), e);
				}
			}
		}
	
	}
	
	
	*/
}
